﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using PMS.XCMG.Util;

namespace PMS.XCMG.Entity
{
    /// <summary>
    /// 产品Bom
    /// </summary>
    public partial class MaterialBom : BusEntity
    {
        /// <summary>
        /// 产品ID
        /// </summary>
        public string ProductId { set; get; }
        /// <summary>
        /// 规格ID
        /// </summary>
        public string SpecId { set; get; }
        /// <summary>
        /// 电气编码
        /// </summary>
        public string EleCode { get; set; }
        /// <summary>
        /// BOM版本
        /// </summary>
        public string Version { set; get; }
        /// <summary>
        /// BOM状态(CF_Enum.Code=State)
        /// </summary>
        public string Status { set; get; }
    }

    public partial class MaterialBom : BusEntity
    {
        /// <summary>
        /// Bom对应的产品实体（产品物料信息）
        /// </summary>
        public MaterielInfor Product { set; get; }
        /// <summary>
        /// Bom对应的规格信息
        /// </summary>
        public MaterielSpec Spec { set; get; }
        /// <summary>
        /// Bom组成清单树
        /// </summary>
        public List<MaterialBomTree> BomTrees { set; get; }
    }

    /// <summary>
    /// 实体配置
    /// OnModelCreating
    /// </summary>
    public class MaterialBomTypeConfig : BusEntityTypeConfig<MaterialBom>, IEntityTypeConfiguration<MaterialBom>
    {
        public override void Configure(EntityTypeBuilder<MaterialBom> builder)
        {
            base.Configure(builder);

            #region 主外键关系
            builder.HasOne(p => p.Product).WithMany().HasForeignKey(p => p.ProductId);
            builder.HasOne(p => p.Spec).WithMany().HasForeignKey(p => p.SpecId);
            #endregion

            #region 字段属性:最大长度,是否必需,默认值
            builder.Property(p => p.ProductId).HasMaxLength(EntityDefinition.DefSmallColLen).IsRequired();
            builder.Property(p => p.SpecId).HasMaxLength(EntityDefinition.DefSmallColLen).IsRequired();
            builder.Property(p => p.EleCode).HasMaxLength(10).IsRequired();
            builder.Property(p => p.Version).HasMaxLength(EntityDefinition.DefLittleColLen).IsRequired();
            builder.Property(p => p.Status).HasMaxLength(EntityDefinition.DefSmallColLen).IsRequired();
            #endregion

            #region 备注
            builder.HasComment("产品Bom");
            builder.Property(p => p.ProductId).HasComment("产品ID-对应物料ID");
            builder.Property(p => p.SpecId).HasComment("规格ID");
            builder.Property(p => p.EleCode).HasComment("电气编码");
            builder.Property(p => p.Version).HasComment("Bom版本");
            builder.Property(p => p.Status).HasComment("BOM状态");
            #endregion

            #region 种子数据
            builder.HasData(new MaterialBom() { Id = "1", ProductId = "10", SpecId = "11", EleCode="1", Status = "Enable", Version = "1.0", CreateUserId = "1", ModifyUserId = "1" });
            builder.HasData(new MaterialBom() { Id = "2", ProductId = "10", SpecId = "12", EleCode = "2", Status = "Enable", Version = "1.0", CreateUserId = "1", ModifyUserId = "1" });
            builder.HasData(new MaterialBom() { Id = "3", ProductId = "10", SpecId = "13", EleCode = "3", Status = "Enable", Version = "1.0", CreateUserId = "1", ModifyUserId = "1" });
            builder.HasData(new MaterialBom() { Id = "4", ProductId = "10", SpecId = "14", EleCode = "4", Status = "Enable", Version = "1.0", CreateUserId = "1", ModifyUserId = "1" });
            builder.HasData(new MaterialBom() { Id = "5", ProductId = "10", SpecId = "15", EleCode = "5", Status = "Enable", Version = "1.0", CreateUserId = "1", ModifyUserId = "1" });
            builder.HasData(new MaterialBom() { Id = "6", ProductId = "10", SpecId = "16", EleCode = "6", Status = "Enable", Version = "1.0", CreateUserId = "1", ModifyUserId = "1" });
            builder.HasData(new MaterialBom() { Id = "7", ProductId = "10", SpecId = "17", EleCode = "7", Status = "Enable", Version = "1.0", CreateUserId = "1", ModifyUserId = "1" });
            builder.HasData(new MaterialBom() { Id = "8", ProductId = "10", SpecId = "18", EleCode = "8", Status = "Enable", Version = "1.0", CreateUserId = "1", ModifyUserId = "1" });
            builder.HasData(new MaterialBom() { Id = "9", ProductId = "10", SpecId = "19", EleCode = "9", Status = "Enable", Version = "1.0", CreateUserId = "1", ModifyUserId = "1" });
            builder.HasData(new MaterialBom() { Id = "10", ProductId = "10", SpecId = "20", EleCode = "10", Status = "Enable", Version = "1.0", CreateUserId = "1", ModifyUserId = "1" });
            #endregion
        }
    }
}
